package com.android.fragmentdemo.definervlayoutmanager;
import android.graphics.Canvas;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

public class SlideItemTouchCallback extends ItemTouchHelper.Callback {

    private static final float DEFAULT_SCALE = 0.85f;
    private final IItemTouchStatus mItemTouchStatus;
    private final int mMaxVisibleCount;

    public SlideItemTouchCallback(IItemTouchStatus mItemTouchStatus, int mMaxVisibleCount) {
        this.mItemTouchStatus = mItemTouchStatus;
        this.mMaxVisibleCount = mMaxVisibleCount;
    }

    @Override
    public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
        int swipeFlag = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
        return makeMovementFlags(0, swipeFlag);
    }

    @Override
    public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
        mItemTouchStatus.onItemRemove(viewHolder.getAdapterPosition());
    }

    @Override
    public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView,
                            @NonNull RecyclerView.ViewHolder viewHolder,
                            float dX, float dY, int actionState, boolean isCurrentlyActive) {
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
        View child = viewHolder.itemView;
        if (ItemTouchHelper.ACTION_STATE_SWIPE == actionState) {
            float ratio = dX / getThreshold(recyclerView, viewHolder);
            if (ratio > 1) {
                ratio = 1;
            } else if (ratio < -1) {
                ratio = -1;
            }
            child.setRotation(ratio * 15);
            for (int i = 0; i < mMaxVisibleCount; i++) {
                View childChild = recyclerView.getChildAt(i);
                if (childChild != null) {
                    float currentScale = (float) Math.pow(DEFAULT_SCALE, 2 - i);
                    float nextScale = currentScale / DEFAULT_SCALE;
                    float scale = nextScale - currentScale;
                    float scaleValue = Math.min(1, currentScale + scale * Math.abs(ratio));
                    childChild.setScaleX(scaleValue);
                    childChild.setScaleY(scaleValue);
                }
            }
        }
    }

    @Override
    public void clearView(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
        super.clearView(recyclerView, viewHolder);
        viewHolder.itemView.setRotation(0f);
    }

    @Override
    public boolean isItemViewSwipeEnabled() {
        return super.isItemViewSwipeEnabled();
    }

    @Override
    public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
        return false;
    }

    private float getThreshold(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        return recyclerView.getWidth() * getSwipeThreshold(viewHolder);
    }
}
